package com.ruoyi.system.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.domain.Vehicle;
import com.ruoyi.system.domain.dto.VehicleDto;
import com.ruoyi.system.service.VehicleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/vehicle")
public class VehicleController {
    @Resource
    VehicleService vehicleService;

    @PreAuthorize(hasPermi = "vehicle:list")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody VehicleDto vehicleDto) {
        return vehicleService.VehicList(vehicleDto);
    }

    /**
     * 修改车辆
     */
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody VehicleDto vehicleDto) {

        return vehicleService.updateVehicle(vehicleDto);
    }

    /**
     * 新增部门
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody VehicleDto vehicleDto) {
        List<Vehicle> list = vehicleService.list();
        for (Vehicle vehicle : list) {
            if (vehicle.getCarNumber().equals(vehicleDto.getCarNumber())) {
                return AjaxResult.error("新增车辆'" + vehicleDto.getCarNumber() + "'失败，车辆名称已存在");
            }
        }


        return vehicleService.addVehicle(vehicleDto);
    }

    @PostMapping("/det")
    public AjaxResult det(@RequestBody VehicleDto vehicleDto) {
        return vehicleService.detVehicle(vehicleDto);
    }

}
